﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace IPR_TrainExamSys_TY.SysSet
{
    public partial class FrmDepartmentSet : Form
    {
        public FrmDepartmentSet()
        {
            InitializeComponent();
        }

        private void FrmDepartmentSet_Load(object sender, EventArgs e)
        {
            initTreeNode();
        }

        private void initTreeNode()
        {
            advTree1.Nodes.Clear();
            #region 初始化菜单和功能栏
            string sWhere = " IsDelete=0 ";
            DataSet dsFun = new T_PublicDepartment().GetList(sWhere);
            if (dsFun != null)
            {
                if (dsFun.Tables.Count > 0)
                    if (dsFun.Tables[0].Rows.Count > 0)
                    {
                        //菜单自定义增加
                        DataRow[] dr = dsFun.Tables[0].Select(" DepartMentPID=0 and DepartMentLevel=0 ", " DepartmentId ASC");
                        for (int i = 0; i < dr.Count(); i++)
                        {
                            DevComponents.AdvTree.Node tnNew = new DevComponents.AdvTree.Node(dr[i]["DepartmentName"].ToString());
                            tnNew.Tag = dr[i]["DepartmentId"].ToString();
                            tnNew.Image = imageList1.Images[int.Parse(dr[i]["DepartMentLevel"].ToString())]; 
                            tnNew.NodeClick += tnNew_NodeClick;
                            AddTreeNode(tnNew, dsFun, dr[i]["DepartmentId"].ToString());
                            advTree1.Nodes.Add(tnNew, DevComponents.AdvTree.eTreeAction.Expand);
                            Application.DoEvents();
                        }
                        advTree1.ExpandAll();
                        
                    }
            }
            #endregion
        }
        /// <summary>
        /// 添加对应的菜单项
        /// </summary>
        /// <param name="bi"></param>
        /// <param name="dsFun"></param>
        /// <param name="DepartmentId"></param>
        private void AddTreeNode(DevComponents.AdvTree.Node myNode, DataSet dsFun, string DepartmentId)
        {
            DataRow[] dr = dsFun.Tables[0].Select(" DepartMentPID='" + DepartmentId + "' ", " DepartmentId ASC");
            if (dr.Count() > 0)
            {
                for (int i = 0; i < dr.Count(); i++)
                {
                    DevComponents.AdvTree.Node tnNew = new DevComponents.AdvTree.Node(dr[i]["DepartmentName"].ToString());
                    tnNew.Tag = dr[i]["DepartmentId"].ToString();
                    tnNew.Image = imageList1.Images[int.Parse(dr[i]["DepartMentLevel"].ToString())]; 
                    tnNew.NodeClick += tnNew_NodeClick;
                    AddTreeNode(tnNew, dsFun, dr[i]["DepartmentId"].ToString());
                    myNode.Nodes.Add(tnNew, DevComponents.AdvTree.eTreeAction.Expand);
                    Application.DoEvents();
                }
            }
        }


        void tnNew_NodeClick(object sender, EventArgs e)
        {
            DevComponents.AdvTree.Node myNode = (DevComponents.AdvTree.Node)sender;
            if (myNode.Level == 0)
            {
                //tbJWD.Text = myNode.Text;
                //lbJWDID.Text = myNode.Tag.ToString();
                lbCurrent.Text = myNode.Text;
            }
            if (myNode.Level == 1)
            {
                //tbJWD.Text = myNode.Parent.Text;
                //tbKS.Text = myNode.Text;
                //lbJWDID.Text = myNode.Parent.Tag.ToString();
                //lbKSID.Text = myNode.Tag.ToString();
                lbCurrent.Text = myNode.Parent.Text + "--" + myNode.Text;
            }
            if (myNode.Level == 2)
            {
                //tbJWD.Text = myNode.Parent.Parent.Text;
                //tbKS.Text = myNode.Parent.Text;
                //tbBZ.Text = myNode.Text;
                //lbJWDID.Text = myNode.Parent.Parent.Tag.ToString();
                //lbKSID.Text = myNode.Parent.Tag.ToString();
                //lbBZID.Text = myNode.Tag.ToString();
                lbCurrent.Text = myNode.Parent.Parent.Text + "--" + myNode.Parent.Text + "--" + myNode.Text;
            }

            lbCurDepartID.Text = myNode.Tag.ToString();
            tbCurDepartName.Text = myNode.Text;
            lbCurLevel.Text = myNode.Level.ToString();

            if (myNode.Parent != null)
                lbParentID.Text = myNode.Parent.Tag.ToString();
            else lbParentID.Text = "0";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (tbCurDepartName.Text.Trim() == "") return;
            try
            {
                Model.T_PublicDepartment mDept = new Model.T_PublicDepartment();
                mDept.DepartmentId = new T_Public().GetMaxID("DepartmentId", "T_PublicDepartment");
                mDept.DepartmentName = tbCurDepartName.Text.Trim();
                mDept.DepartMentPID = Int32.Parse(lbParentID.Text);
                mDept.DepartMentLevel = Int32.Parse(lbCurLevel.Text);
                mDept.IsDelete = 0;

                new T_PublicDepartment().Add(mDept);
                initTreeNode();
            }
            catch (Exception exc)
            {
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (tbCurDepartName.Text.Trim() == "") return;
            try
            {
                Model.T_PublicDepartment mDept = new Model.T_PublicDepartment();
                mDept.DepartmentId = new T_Public().GetMaxID("DepartmentId", "T_PublicDepartment");
                mDept.DepartmentName = tbCurDepartName.Text.Trim();
                mDept.DepartMentPID = Int32.Parse(lbCurDepartID.Text);
                mDept.DepartMentLevel = Int32.Parse(lbCurLevel.Text)+1;
                mDept.IsDelete = 0;

                new T_PublicDepartment().Add(mDept);
                initTreeNode();
            }
            catch (Exception exc)
            {
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tbCurDepartName.Text.Trim() == "") return;
            try
            {
                string sql = "update T_PublicDepartment set DepartmentName='"+tbCurDepartName.Text+"' where DepartmentId="+lbCurDepartID.Text+"";
                new T_Public().ExecuteSql(sql);
                initTreeNode();
            }
            catch (Exception exc)
            {
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (tbCurDepartName.Text.Trim() == "") return;
            try
            {
                string sql = "delete from T_PublicDepartment  where DepartmentId=" + lbCurDepartID.Text.Trim() + " or DepartMentPID= " + lbCurDepartID.Text.Trim();
                new T_Public().ExecuteSql(sql);
                initTreeNode();
            }
            catch (Exception exc)
            {
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
